package com.lc.ibps.platform.rest.system;

import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lc.ibps.api.common.cat.model.IType;
import com.lc.ibps.api.common.cat.service.ITypeService;
import com.lc.ibps.base.core.util.AppUtil;
import com.lc.ibps.base.core.util.JacksonUtil;
import com.lc.ibps.base.core.util.string.StringUtil;
import com.lc.ibps.cloud.entity.APIResult;
import com.lc.ibps.common.cat.persistence.entity.TypePo;
import com.lc.ibps.common.cat.repository.TypeRepository;
import com.lc.ibps.web.controller.BaseApiController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 系统分类标识接口
 *
 * <pre>
 *  
 * 构建组：ibps-platform-webapi
 * 作者：zhongjh
 * 邮箱：zjh20140614@163.com
 * 日期：2017年8月28日-下午6:13:53
 * 版权：广州流辰信息技术有限公司版权所有
 * </pre>
 */
@RequestMapping(value="/api/webapi/typeService")
@Api(value = "/typeService", tags = "系统管理-分类服务")
@RestController
public class TypeController extends BaseApiController {

	public ITypeService typeService;
	public TypeRepository typeRepository;

	public TypeController() {
		typeService = AppUtil.getBean(ITypeService.class);
		typeRepository = AppUtil.getBean(TypeRepository.class);
	}

	@ApiOperation(value = "系统分类类型", notes = "系统分类类型")
	@RequestMapping(value="/getTypes",method=RequestMethod.GET)
	public APIResult<List<IType>> getTypeByTypeId(
			@ApiParam(name = "catKey", value = "系统分类key值", required = true) @RequestParam(name = "catKey", required = true) String catKey) {
		APIResult<List<IType>> result = new APIResult<>();
		if (StringUtil.isEmpty(catKey)) {
			return getResult(result, FAIL, "缺少参数，请正确输入参数！");
		}
		try {
			List<IType> typePo = typeService.getByCatKey(catKey);
			result.setData(typePo);
			result.setMessage("获取系统分类类型成功！");
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取分类类型失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "系统分类类型", notes = "系统分类类型")
	@RequestMapping(value="/getByCatKeyPid",method=RequestMethod.GET)
	public APIResult<List<TypePo>> getByCatKeyPid(
			@ApiParam(name="catKey", value = "系统分类key值", required = true) 
			@RequestParam(name = "catKey", required = true) String catKey,
			@ApiParam(name="pid", value = "父节点Id", required = true)
			@RequestParam(name = "pid", required = true) String pid) {
		APIResult<List<TypePo>> result = new APIResult<>();
		if (StringUtil.isEmpty(catKey) || StringUtil.isEmpty(pid)) {
			return getResult(result, FAIL, "缺少参数，请正确输入参数！");
		}
		try {
			String listData = typeService.findByCategoryKeyPid(catKey, pid);
			if (JacksonUtil.isJsonArray(listData)) {
				List<TypePo> list = JacksonUtil.getDTOList(listData, TypePo.class);
				result.setData(list);
			}
			result.setMessage("获取分类类型成功！");
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取分类类型失败！", e);
			
		}
		return result;
	}
	
	@ApiOperation(value = "系统分类类型", notes = "获取分类子项")
	@RequestMapping(value="/getType",method=RequestMethod.GET)
	public APIResult<TypePo> getType(
			@ApiParam(name="id", value = "节点Id", required = true)
			@RequestParam(name = "id", required = true) String id) {
		APIResult<TypePo> result = new APIResult<>();
		try {
			TypePo po = typeRepository.get(id);
			result.setData(po);
			result.setMessage("获取分类子项成功！");
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取分类子项失败！", e);
			
		}
		return result;
	}

}
